package com.maochang.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import java.util.List;

//表示是配置类
@Configuration
//解析  spring  的ico 注解
@ComponentScan( basePackages = {"com.maochang.controller","com.maochang.service","com.maochang.controller.fitter"})
//启用 spirng Mvc
@EnableWebMvc  //对应两个解析器    //相当于xml 配置的什么
// handlerMapping   :用来解析映射注解
// handlerAdapter   :数据的类型转换
/**
 * 会自动注册这三个类
 * RequestMappingHandlerMapping
 * RequestMappingHandlerAdapter
 * ExceptionHandlerExceptionResolver
 */
//拥有 RequestMapping 的方法称为 handler
                                        //这里可以   实现   webMvcConfigurer
                                        //也可以     继承   webMvcConfigurationSupport
public class SpringMvcJConfig implements WebMvcConfigurer {
    /**
     * jsp视图解析器视图解析器
     * @return
     */
    //@Bean
    //public InternalResourceViewResolver viewResolver(){
    //   InternalResourceViewResolver internalResourceViewResolver=new InternalResourceViewResolver();
    //   internalResourceViewResolver.setPrefix("/WEB-INF/views/");
    //   internalResourceViewResolver.setSuffix(".jsp");
    //   return  internalResourceViewResolver;
    //}
    //消息转换器   配置全局信息   response.setContentType("application/json;charset=UTF-8");
    //@Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //json乱码全局配置
        //StringHttpMessageConverter  Jackconverter=new StringHttpMessageConverter();
        //Jackconverter.setDefaultCharset(Charset.forName("UTF-8"));
        //converters.add(Jackconverter);
        //spirng mvc jackson 消息转换器
        MappingJackson2HttpMessageConverter messageConverter=new MappingJackson2HttpMessageConverter();
        converters.add(messageConverter);
    }

    //配置静态资源处理方式2
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
                                                    //访问虚拟路径                              //实际路径
        registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/static/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/");
        registry.addResourceHandler("/admin/**").addResourceLocations("/WEB-INF/admin/");
    }
    //模板解析器
    @Bean
    public SpringResourceTemplateResolver springResourceTemplateResolver(){
        SpringResourceTemplateResolver s=new SpringResourceTemplateResolver();
        s.setPrefix("/WEB-INF/templates/");
        s.setSuffix(".html");
        s.setCharacterEncoding("UTF-8");
        s.setOrder(1);
        s.setTemplateMode("HTML5");
        s.setCacheable(false);
        return s;
    }
    @Bean
    public SpringTemplateEngine templateEngine(){
        SpringTemplateEngine s=new SpringTemplateEngine();
        s.setTemplateResolver(springResourceTemplateResolver());
        return s;
    }

    @Bean
    public ThymeleafViewResolver thymeleafViewResolver(){
        ThymeleafViewResolver t=new ThymeleafViewResolver();
        t.setTemplateEngine(templateEngine());
        t.setCharacterEncoding("UTF-8");
        return t;
    }

    //文件上传，bean必须写name属性且必须为multipartResolver，不然取不到文件对象，别问我为什么，我也唔知
    //@Bean(name="multipartResolver")
    //protected CommonsMultipartResolver MultipartResolver() {
    //    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    //    //multipartResolver.setUploadTempDir(new FileSystemResource("/tmp"));//可不设置
    //    multipartResolver.setMaxUploadSize(2097152);//2M
    //    multipartResolver.setMaxInMemorySize(0);
    //    multipartResolver.setDefaultEncoding("UTF-8");
    //    return multipartResolver;
    //}

}
